<script setup lang="ts">
import { onMounted } from 'vue';
import { navigateTo, reLaunch, showLoading } from '@tarojs/taro';
import { EMERGENCY_DRILL_QUERY_APP } from '/@/api/modules/base/emergency-drill';
import { state } from './state';
import dayjs from 'dayjs';
/**
 * @description 消息列表查询
 * @param {Number} page -当前页
 * @param {Number} pageSize -每页条数
 */
function queryList(page: number = 1, pageSize: number = 15) {
  if (!page && state.finished) return;
  state.loading = true;
  showLoading();
  EMERGENCY_DRILL_QUERY_APP({
    comQuery: { page, pageSize },
    dto: {},
  })
    .then((res) => {
      state.loading = false;

      if (
        state.activePage >=
        (res.comQuery?.pageCount ||
          (res.comQuery?.totalCount as number) / (res.comQuery?.pageSize as number) ||
          1)
      )
        state.finished = true;
      if (!res.dto || !res.dto.length) return;

      state.list.push(...res.dto);
      state.activePage += 1;
    })
    .catch(() => {
      state.loading = false;
    });
}

/**
 * @function 加载
 */
function onLoading() {
  if (state.finished) return;

  queryList(state.activePage);
}

onMounted(() => {
  state.activePage = 1;
  state.loading = true;
  state.finished = false;
  state.list = [];
  onLoading();
});
</script>

<template>
  <view class="drill-list app-page--1">
    <app-navbar title="应急管理" />
    <nut-tabs
      model-value="drill"
      @click="
        ({ paneKey }) => {
          if (paneKey === 'plan') {
            reLaunch({ url: '/pages/emergency-manage/plan/list/index' });
          }
        }
      "
    >
      <nut-tab-pane title="应急预案" pane-key="plan" />
      <nut-tab-pane title="应急演练" pane-key="drill" />
    </nut-tabs>
    <scroll-view
      class="app-page__wrapper"
      :scroll-y="true"
      :lower-threshold="100"
      @scrolltolower="onLoading()"
    >
      <view class="drill-list__wrapper">
        <view
          class="drill-list__item"
          v-for="item in state.list"
          :key="item.id"
          @click="
            navigateTo({
              url: `/pages/emergency-manage/drill/detail/index?id=${item.id}`,
            })
          "
        >
          <view class="drill-list__item__header">
            <view class="drill-list__item__header--left">{{ item.name }}</view>
            <view
              class="drill-list__item__header__status"
              :class="{ 'drill-list__item__header__status-complete': item.status === 2 }"
              >{{ item.status === 2 ? '已结束' : '未完成' }}</view
            >
          </view>
          <view
            >{{ dayjs(item.beginTime).format('YYYY-MM-DD HH:mm') }} ~
            {{ dayjs(item.endTime).format('YYYY-MM-DD HH:mm') }}</view
          >
        </view>

        <view class="app-page__list-tips">
          <text v-if="!state.list.length">暂无数据！</text>
          <text v-else-if="state.list.length && state.loading">加载中...</text>
          <text v-else-if="state.list.length && state.finished">加载完成!</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
@import './index.scss';
</style>
